package handler

import (
	"catering_sys_api/cmd/api/biz/business/model/store"
	"catering_sys_api/cmd/api/biz/business/service"
	"catering_sys_api/cmd/api/model"
	"catering_sys_api/core/errno"
	"context"

	"github.com/cloudwego/hertz/pkg/app"
)

type StoreHandler struct {
}

// 该商户下的店铺的列表
func (s *StoreHandler) List(ctx context.Context, c *app.RequestContext) {
	var (
		request      store.ListRequest
		list         []store.ListResponse
		pagination   *model.Pagination
		err          error
		storeService *service.StoreService
	)
	err = c.Bind(&request)
	if err != nil {
		errno.Fail(errno.FAIL_ERR, "", err.Error(), ctx, c)
		return
	}
	storeService = service.NewStoreService(ctx, c)
	list, pagination, err = storeService.List(request)

	if err != nil {
		errno.Fail(errno.FAIL_ERR, "", "fail", ctx, c)
		return
	}
	errno.Ok(map[string]interface{}{"list": list, "pagination": pagination}, "", ctx, c)
	return
}

// add
func (s *StoreHandler) Add(ctx context.Context, c *app.RequestContext) {
	var (
		err          error
		request      store.AddRequest
		storeService *service.StoreService
	)
	err = c.BindAndValidate(&request)
	if err != nil {
		errno.Fail(errno.HTTP_PARAM_MISS, "", err.Error(), ctx, c)
		return
	}
	storeService = service.NewStoreService(ctx, c)
	err = storeService.Add(request)
	if err != nil {
		errno.Fail(errno.DATA_ADD_FAIL, "", err.Error(), ctx, c)
		return
	}
	errno.Ok("", "", ctx, c)
	return
}

// edit
func (s *StoreHandler) Update(ctx context.Context, c *app.RequestContext) {
	var (
		err     error
		request store.UpdateRequest
		//response     store.UpdateResponse
		storeService *service.StoreService
	)
	err = c.BindAndValidate(&request)
	if err != nil {
		errno.Fail(errno.HTTP_PARAM_MISS, "", err.Error(), ctx, c)
		return
	}
	storeService = service.NewStoreService(ctx, c)
	err = storeService.Update(request)
	if err != nil {
		errno.Fail(errno.DATA_UPDATE_FAIL, "", err.Error(), ctx, c)
		return
	}
	errno.Ok("", "", ctx, c)
	return
}

// Detail
func (s *StoreHandler) Detail(ctx context.Context, c *app.RequestContext) {
	var (
		err          error
		request      store.DetailRequest
		response     store.DetailResponse
		storeService *service.StoreService
	)
	err = c.BindAndValidate(&request)
	if err != nil {
		errno.Fail(errno.HTTP_PARAM_MISS, "", err.Error(), ctx, c)
		return
	}
	storeService = service.NewStoreService(ctx, c)
	response, err = storeService.Detail(request)
	if err != nil {
		errno.Fail(errno.DATA_NO_FUND, "", err.Error(), ctx, c)
		return
	}
	errno.Ok(response, "", ctx, c)
	return
}

// state
func (s *StoreHandler) State(ctx context.Context, c *app.RequestContext) {
	var (
		err          error
		request      store.StateRequest
		storeService *service.StoreService
	)
	err = c.BindAndValidate(&request)
	if err != nil {
		errno.Fail(errno.HTTP_PARAM_MISS, "", err.Error(), ctx, c)
		return
	}
	storeService = service.NewStoreService(ctx, c)
	err = storeService.State(request)
	if err != nil {
		errno.Fail(errno.DATA_UPDATE_FAIL, "", err.Error(), ctx, c)
		return
	}
	errno.Ok("", "", ctx, c)
	return
}
